package com.zzz.system.service.question.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zzz.common.core.enums.ResultCode;
import com.zzz.common.security.exception.ServiceException;
import com.zzz.system.domain.question.Question;
import com.zzz.system.domain.question.dto.QuestionAddDTO;
import com.zzz.system.domain.question.dto.QuestionEditDto;
import com.zzz.system.domain.question.dto.QuestionQueryDTO;
import com.zzz.system.domain.question.vo.QuestionDetailVO;
import com.zzz.system.domain.question.vo.QuestionVo;
import com.zzz.system.mapper.question.QuestionMapper;
import com.zzz.system.service.question.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.system.service.question.impl
 * @Author: zhai
 * @CreateTime: 2025-10-15  20:16
 * @Description: TODO 问题服务实现类
 * @Version: 1.0
 */
@Slf4j
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * @description: 获取问题列表
     * @author: zhai
     * @date: 2025/10/19 13:09
     * @param: [questionQueryDTO]
     * @return: java.util.List<com.zzz.system.domain.question.vo.QuestionVo>
     **/
    @Override
    public List<QuestionVo> list(QuestionQueryDTO questionQueryDTO) {
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        List<QuestionVo> questionVOList = questionMapper.selectQuestionList(questionQueryDTO);
        return questionVOList;
    }

    /**
     * @description: 添加问题
     * @author: zhai
     * @date: 2025/10/19 13:08
     * @param: [questionAddDTO]
     * @return: boolean
     **/
    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        log.info("添加问题：{}", questionAddDTO);
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if (!CollectionUtil.isEmpty(questionList)) {
            log.error("添加问题失败，问题已存在");
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        log.info("可以添加题目，开始添加题目");
        Question question = new Question();
        // 使用BeanUtils.copyProperties将questionAddDTO中的属性复制到question对象中
        BeanUtils.copyProperties(questionAddDTO, question);
        log.info("此时的question：{}", question);
        int insert = questionMapper.insert(question);
        if (insert <= 0) {
            log.error("添加问题失败");
            return false;
        }
        log.info("添加问题成功：{}", questionAddDTO);
        return true;
    }

    /**
     * @description: 获取问题详细信息
     * @author: zhai
     * @date: 2025/10/19 12:50
     * @param: [questionEditDto]
     * @return: com.zzz.system.domain.question.vo.QuestionDetailVO
     **/
    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            log.error(ResultCode.FAILED_FIND_QUESTION.getMsg());
            throw new ServiceException(ResultCode.FAILED_FIND_QUESTION);
        }
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtils.copyProperties(question, questionDetailVO);
        log.info("找到该问题的详细信息返回：{}", questionDetailVO);
        return questionDetailVO;
    }

    /**
     * @description: 编辑问题
     * @author: zhai
     * @date: 2025/10/19 14:29
     * @param: [questionId]
     * @return: int
     **/
    @Override
    public int edit(QuestionEditDto questionEditDto) {
        Question oldQuestion = questionMapper.selectById(questionEditDto.getQuestionId());
        if (oldQuestion == null) {
            log.error(ResultCode.FAILED_FIND_QUESTION.getMsg());
            throw new ServiceException(ResultCode.FAILED_FIND_QUESTION);
        }
        oldQuestion.setTitle(questionEditDto.getTitle());
        oldQuestion.setDifficulty(questionEditDto.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDto.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDto.getSpaceLimit());
        oldQuestion.setContent(questionEditDto.getContent());
        oldQuestion.setQuestionCase(questionEditDto.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDto.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDto.getMainFuc());
        int fact = questionMapper.updateById(oldQuestion);
        if (fact <= 0) {
            log.error(ResultCode.FAILED_EDIT_QUESTION.getMsg());
            throw new ServiceException(ResultCode.FAILED_EDIT_QUESTION);
        }
        log.info("修改问题成功：{}", questionEditDto);
        return 1;
    }

    /**
     * @description: 删除题目
     * @author: zhai
     * @date: 2025/10/19 15:27
     * @param: [questionId]
     * @return: int
     **/
    @Override
    public int delete(Long questionId) {
        if (questionId == null) {
            log.error(ResultCode.FAILED_PARAMS_ERROR.getMsg());
            throw new ServiceException(ResultCode.FAILED_PARAMS_ERROR);
        }
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            log.error(ResultCode.FAILED_NOT_EXISTS.getMsg());
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        int delete = questionMapper.deleteById(questionId);
        if (delete <= 0) {
            log.error(ResultCode.FAILED_DELETE.getMsg());
            throw new ServiceException(ResultCode.FAILED_DELETE);
        }
        log.info("删除问题成功：{}", questionId);
        return 1;
    }
}
